﻿using System.Web.Mvc;
using System.Web.Security;
using web_uchet.Security;
using Mvc.Validation;
using web_uchet.Classes.Security;
using web_uchet.Models;
namespace web_uchet.Controllers
{

    public class AccountController : MasterPageController
    {
        public AccountController()
            : this(null)
        {
        }

        public AccountController(IUserOperationsProvider Provider)
            : base(Provider)
        {
            
        }
        [Authorize(Roles="Администратор")]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult RegisterUser()
        {
            ViewUser NewUser = new ViewUser(new Пользователь(), false);
            ViewData.Add("WorkplaceID", this._UserOperationProvider.GetCurrentCompanyWorkPlacesList(new int?()));
            ViewData.Add("Role1", this._UserOperationProvider.GetUserRolesList(null));
            ViewData.Add("Role2", this._UserOperationProvider.GetUserRolesList(null));
            ViewData.Add("Role3", this._UserOperationProvider.GetUserRolesList(null));
            return View(NewUser);
        }
        [Authorize(Roles = "Администратор")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult RegisterUser(ViewUser NewUser, string EnteredChars, string EncChars)
        {    
            if (EnterCharsProvider.Validate(EnteredChars, EncChars))
            {
            try
            {
                NewUser.Validate();
                this._UserOperationProvider.CreateUser(NewUser, false);
                return View("Success");
            }
            catch (EntityValidationException ex)
            {
                ViewData.Add("WorkplaceID", this._UserOperationProvider.GetCurrentCompanyWorkPlacesList(new int?()));
                ViewData.Add("Role1", this._UserOperationProvider.GetUserRolesList(null));
                ViewData.Add("Role2", this._UserOperationProvider.GetUserRolesList(null));
                ViewData.Add("Role3", this._UserOperationProvider.GetUserRolesList(null));
                ViewData.ModelState.PopulateWithErrors(ex);
                return View(NewUser);
            }
            }
            else
            {
                ViewData.ModelState.AddModelError("EnteredChars", "Неверные символы");
                return View(NewUser);
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ChangePassword()
        {
            return View(new ChangePassword());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChangePassword(ChangePassword Changes)
        {
            try
            {
                Changes.Validate();
                this._UserOperationProvider.ChangePassword(Changes);
                return View("Success");
            }
            catch (EntityValidationException ex)
            {
                ViewData.ModelState.PopulateWithErrors(ex);
                return View(Changes);
            }
        }       

        //[AcceptVerbs(HttpVerbs.Get)]
        //public ActionResult ForgotPassword(ForgotPassword FP)
        //{
            
        //    return View(FP);
        //}
        //[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ForgotPassword(ForgotPassword FP)
        {
            if (Request.IsAjaxRequest())
            {
                FP.Pquestion = Membership.GetUser(FP.UserName).PasswordQuestion;                
                if (FP.Panswer != string.Empty)
                {
                    try
                    {
                        FP.Password = Membership.GetUser(FP.UserName).GetPassword(FP.Panswer);
                    }
                    catch (MembershipPasswordException)
                    { return View(FP); }
                }
                if (FP.Panswer != null)
                {
                    try
                    {
                        FP.Password = Membership.GetUser(FP.UserName).GetPassword(FP.Panswer);
                    }
                    catch (MembershipPasswordException)
                    { return View(FP); }
                }
                return Json(FP, JsonRequestBehavior.AllowGet);
            }            
            return View(FP);            
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult UserNotExists(string UserName)
        {
            return Json(this._UserOperationProvider.UserNotExistst(UserName),JsonRequestBehavior.AllowGet);
        }
        
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult GetEnterChars()
        {
            if (Request.IsAjaxRequest())
            {
                ViewData.Add("EncChars", EnterCharsProvider.GenChars().Encrypted);
                return View("EnterCharsPartial");
            }
                else return new EmptyResult();
        }
    }

}
